using System;
using System.Collections.Generic;
using System.Text;

namespace CommandPatternApp
{
    public class UndoCmd : BaseCmd
    {
        private Stack<UndoableCmd> _cmdStack;
        private CmdMenuItem _undoMenuItem;

        public UndoCmd(TextEditor editor, CmdFactory factory, CmdMenuItem undoMenuItem)
            : base(editor, factory)
        {
            _undoMenuItem = undoMenuItem;
            _cmdStack = new Stack<UndoableCmd>();
        }

        public override void Execute()
        {
            UndoableCmd cmd = _cmdStack.Pop();
            cmd.Undo();
            if (_cmdStack.Count == 0)
            {
                _undoMenuItem.Visible = false;
            }
            else
            {
                _undoMenuItem.Visible = true;
                _undoMenuItem.Text = "Undo " + _cmdStack.Peek().Name;
            }
        }

        public void Push(UndoableCmd cmd)
        {
            _cmdStack.Push(cmd);
            _undoMenuItem.Visible = true;
            _undoMenuItem.Text = "Undo " + cmd.Name;
        }
    }
}
